package model;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Observable;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;

/**
 *
 */
public class XMLLoadModel extends Observable {
    private XMLContentHandler handler;
    
    public XMLLoadModel() {
        this.handler = new XMLContentHandler();
    }

    public void parse(String url) throws SAXException, FileNotFoundException, IOException {
        //load xml file from url
        URL uri = new URL(url);
        URLConnection connection = uri.openConnection();
        connection.setRequestProperty("User-Agent", "Patrick Bauer, s0528597");
        InputSource inputSource = new InputSource(connection.getInputStream());

        //instance xmlreader
        XMLReader xmlReader = XMLReaderFactory.createXMLReader();
        xmlReader.setContentHandler(this.handler);
        xmlReader.parse(inputSource);
        
        //notify
        this.update();
    }

    private void update() {
        this.setChanged();
        this.notifyObservers(this.handler.getUebungen());
    }
   
}
